package com.leyou.page.mq;

import com.leyou.common.constants.MQConstants;
import com.leyou.page.service.PageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 商品上架->生成静态页
 *    consumerGroup: 消费组名称
 *    topic: 需要监听的消息主题
 *    selectorExpression: 标签的表达式
 *    messageModel: 消息模型
 *        MessageModel.CLUSTERING： 集群的轮询，所有同名消息组下的消费者轮询接受
 *        MessageModel.BROADCASTING： 广播，所有同名消息组下的消费者同时接受
 */
@Component  //注意：让这个监听器生效
@RocketMQMessageListener(
        consumerGroup="itemUpConsumerGroup",
        topic= MQConstants.Topic.ITEM_TOPIC_NAME,
        selectorExpression = MQConstants.Tag.ITEM_UP_TAG,
        messageModel = MessageModel.BROADCASTING
)
@Slf4j
public class ItemUpListener implements RocketMQListener<Long> {

    @Autowired
    private PageService pageService;

    @Override
    public void onMessage(Long id) {
        //调用业务，生成静态页
        try {
            pageService.createStaticPage(id);
            log.info("【静态页同步】静态页成功产生。id为"+id);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【静态页同步】静态页生成失败。id为"+id+"。原因："+e.getMessage());
        }

    }
}
